package com.yanqu.road.dao.impl.activity.langyamemorial;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.langyamemorial.CrossLangYaMemorialAllData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class CrossLangYaMemorialAllDataDaoImpl extends TempDataObjectDao<CrossLangYaMemorialAllData> {


    public static String getCrossLangYaMemorialAllDataSql = "select * from `t_c_activity_langya_memorial_all_data` where `activityId`=?";
    public CrossLangYaMemorialAllData getCrossLangYaMemorialAllData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossLangYaMemorialAllDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateCrossLangYaMemorialAllDataSql = "update `t_c_activity_langya_memorial_all_data` set buildValue=?,updateTime=?,buildTimes=? where activityId=?";
    @Override
    protected boolean updateData(CrossLangYaMemorialAllData crossLangYaMemorialAllData) {
        crossLangYaMemorialAllData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, crossLangYaMemorialAllData.getBuildValue()));
        para.put(i++, new DbParameter(Types.BIGINT, crossLangYaMemorialAllData.getUpdateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossLangYaMemorialAllData.getBuildTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, crossLangYaMemorialAllData.getActivityId()));
        if (getDb().executeNoneQuery(updateCrossLangYaMemorialAllDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossLangYaMemorialAllDataSql = "insert into `t_c_activity_langya_memorial_all_data`(activityId,buildValue,updateTime,buildTimes) values (?,?,?,?)";
    @Override
    protected boolean addData(CrossLangYaMemorialAllData crossLangYaMemorialAllData) {
        crossLangYaMemorialAllData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossLangYaMemorialAllData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossLangYaMemorialAllData.getBuildValue()));
        para.put(i++, new DbParameter(Types.BIGINT, crossLangYaMemorialAllData.getUpdateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossLangYaMemorialAllData.getBuildTimes()));
        if (getDb().executeNoneQuery(insertCrossLangYaMemorialAllDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossLangYaMemorialAllData parseData(ResultSet rs) throws SQLException {
        CrossLangYaMemorialAllData data = new CrossLangYaMemorialAllData();
        data.setActivityId(rs.getInt("activityId"));
        data.setBuildValue(rs.getLong("buildValue"));
        data.setUpdateTime(rs.getLong("updateTime"));
        data.setBuildTimes(rs.getLong("buildTimes"));

        return data;
    }
}
